package com.github.pig.admin.service.park.impl;

import com.baomidou.mybatisplus.mapper.EntityWrapper;
import com.baomidou.mybatisplus.mapper.Wrapper;
import com.baomidou.mybatisplus.service.impl.ServiceImpl;
import com.github.pig.admin.mapper.park.EmployeeInfoMapper;
import com.github.pig.admin.model.entity.SysDept;
import com.github.pig.admin.model.park.EmployeeInfo;
import com.github.pig.admin.service.park.IEmployeeInfoService;
import com.github.pig.admin.service.sys.SysDeptService;
import com.github.pig.common.constant.CommonConstant;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.Date;
import java.util.List;

/**
 * <p>
 *  服务实现类
 * </p>
 *
 * @author sxf
 * @since 2018-12-17
 */
@Service
public class EmployeeInfoServiceImpl extends ServiceImpl<EmployeeInfoMapper, EmployeeInfo> implements IEmployeeInfoService {
    @Autowired
    private SysDeptService deptService;

    /**
     * 企業總人數+1
     * 插入企業ID
     * 设APP登陆默认企业
     * 添加用户 根据人事档案职位设置用户类型 配置APP权限
     * @param employeeInfo
     * @return
     */
    @Override
    @Transactional(rollbackFor = Exception.class)
    public boolean unionInsert(EmployeeInfo employeeInfo) {
        SysDept dept = deptService.selectById(employeeInfo.getDeptId());
        if(dept!=null){
            employeeInfo.setCompanyId(dept.getCompanyId());
            SysDept company =  deptService.selectById(dept.getCompanyId());
            if(company!=null){
                company.setUpdateTime(new Date());
                company.setEmployeeNumber(company.getEmployeeNumber()+1);
                //deptService.updateDeptById(company);
                deptService.updateById(company);
            }
        }
        //新增用户

        //默认值
        employeeInfo.setIsDefault("1");
        return this.insert(employeeInfo);
    }

    @Override
    public boolean checkDuplicate(String phone, Integer companyId) {
        Wrapper wrapper = new EntityWrapper();
        wrapper.eq("phone",phone);
        wrapper.eq("company_id",companyId);
        wrapper.eq(CommonConstant.DEL_FLAG,CommonConstant.STATUS_NORMAL);
        List list = this.selectList(wrapper);
        if(list!=null && list.size()>0){
            return true;
        }
        return false;
    }
}
